#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sock = new QTcpSocket();
    sock->connectToHost("192.168.199.1", 8899);
    qDebug() << "connect over";
    connect(sock, SIGNAL(connected()), this, SLOT(handConnected()));
    qDebug() << "find over";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_closebutton_clicked()
{

}

void MainWindow::on_loginbutton_clicked()
{
   QString uName = ui->name->text();
   QString pWord = ui->passwd->text();
   // #02|xxx|xxxx
   QString sendData = "#02|" + uName + "|" + pWord;
   qDebug() << sendData;
   sock->write(sendData.toUtf8());
   qDebug() << "over";
}

void MainWindow::handConnected()
{
    ui->loginbutton->setEnabled(true);
    connect(sock, SIGNAL(readyRead()), this, SLOT(hadread()));
}
void MainWindow::hadread()
{
    QByteArray recvArray = sock->readAll();

    QString recvStr(recvArray);

    //#02|0|success

    QStringList recvList = recvStr.split('|');

    if(recvList[1] == '0')
    {
        qDebug() << recvList[2];
        QMessageBox::information(this, "提示", "登陆成功");
        ope *operation = new ope(sock);
        operation->show();
        this->hide();
        disconnect(sock, SIGNAL(readyRead()), 0, 0);
    }
    else
    {
        qDebug() << recvList[2];
        QMessageBox::information(this, "警告", recvList[2]);
    }

}
